//-----------------------------------------------------------------------
// <copyright file="UserPermissionDAOBase.cs" company="FetCo">
//     Copyright (c) FetCo. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

using System;
using System.Data;
using System.Data.Common;
using System.Collections;
using FetCo.Framework;
using FetCo.Framework.Data;
using Microsoft.Practices.EnterpriseLibrary.Data;
using ADESSA.Entities;

namespace ADESSA.Data
{    
    public abstract class UserPermissionDAOBase
    {
        #region Common methods
        public virtual UserPermission CreateUserPermissionFromReader(IDataReader reader)
        {
            UserPermission item = new UserPermission();
            try
            {
                if (!reader.IsDBNull(reader.GetOrdinal("UserPermissionId"))) item.UserPermissionId = (int)reader["UserPermissionId"];
                if (!reader.IsDBNull(reader.GetOrdinal("UserId"))) item.UserId = (long)reader["UserId"];
                if (!reader.IsDBNull(reader.GetOrdinal("PermissionId"))) item.PermissionId = (int)reader["PermissionId"];
                if (!reader.IsDBNull(reader.GetOrdinal("CategoryId"))) item.CategoryId = (string)reader["CategoryId"];
                if (!reader.IsDBNull(reader.GetOrdinal("SubCategoryID"))) item.SubCategoryID = (long)reader["SubCategoryID"];
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessCreateUserPermissionFromReaderException, ex);
            }
            return item;
        }
        #endregion
        
        #region CreateUserPermission methods
            
        public virtual void CreateUserPermission(UserPermission userPermission)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spUserPermissionInsert");
                
                database.AddInParameter(dbCommand, "@UserId", DbType.Int64, userPermission.UserId);
                database.AddInParameter(dbCommand, "@PermissionId", DbType.Int32, userPermission.PermissionId);
                database.AddInParameter(dbCommand, "@CategoryId", DbType.String, userPermission.CategoryId);
                database.AddInParameter(dbCommand, "@SubCategoryID", DbType.Int64, userPermission.SubCategoryID);
                database.AddOutParameter(dbCommand, "@UserPermissionId", DbType.Int32, 4);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommand);
                userPermission.UserPermissionId = (int)database.GetParameterValue(dbCommand, "@UserPermissionId");
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessCreateUserPermissionException, ex);
            }
        }

        #endregion

        #region UpdateUserPermission methods
        
        public virtual void UpdateUserPermission(UserPermission userPermission)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spUserPermissionUpdate");            
                
                database.AddInParameter(dbCommand, "@UserPermissionId", DbType.Int32, userPermission.UserPermissionId);
                database.AddInParameter(dbCommand, "@UserId", DbType.Int64, userPermission.UserId);
                database.AddInParameter(dbCommand, "@PermissionId", DbType.Int32, userPermission.PermissionId);
                database.AddInParameter(dbCommand, "@CategoryId", DbType.String, userPermission.CategoryId);
                database.AddInParameter(dbCommand, "@SubCategoryID", DbType.Int64, userPermission.SubCategoryID);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommand);
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessUpdateUserPermissionException, ex);
            }
        }
        
        #endregion

        #region DeleteUserPermission methods
        public virtual void DeleteUserPermission(int userPermissionId)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spUserPermissionDelete");
                
                database.AddInParameter(dbCommand, "@UserPermissionId", DbType.Int32, userPermissionId);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommand);
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessDeleteUserPermissionException, ex);
            }
        }

        #endregion

        #region GetUserPermission methods
        
        public virtual UserPermission GetUserPermission(int userPermissionId)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spUserPermissionGet");
                
                database.AddInParameter(dbCommand, "@UserPermissionId", DbType.Int32, userPermissionId);
                
                UserPermission userPermission =  null;
                using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommand))
                {
                    if (reader.Read())
                    {
                        userPermission = CreateUserPermissionFromReader(reader);
                        reader.Close();
                    }
                }
                return userPermission;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessGetUserPermissionException, ex);
            }
        }
        
        #endregion

        #region GetUserPermissionList methods
        public virtual UserPermissionCollection GetUserPermissionList(UserPermissionColumns orderBy, OrderDirection orderDirection, int page, int pageSize, out int totalRecords)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spUserPermissionGetList");
                
                database.AddInParameter(dbCommand, "@OrderBy", DbType.AnsiString, orderBy.ToString());
                database.AddInParameter(dbCommand, "@OrderDirection", DbType.AnsiString, orderDirection.ToString());
                database.AddInParameter(dbCommand, "@Page", DbType.Int32, page);
                database.AddInParameter(dbCommand, "@PageSize", DbType.Int32, pageSize);
                database.AddOutParameter(dbCommand, "@TotalRecords", DbType.Int32, 4);
                
                UserPermissionCollection userPermissionCollection = new UserPermissionCollection();
                using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommand))
                {
                    while (reader.Read())
                    {
                        UserPermission userPermission = CreateUserPermissionFromReader(reader);
                        userPermissionCollection.Add(userPermission);
                    }
                    reader.Close();
                }
                totalRecords = (int)database.GetParameterValue(dbCommand, "@TotalRecords");
                return userPermissionCollection;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessGetUserPermissionListException, ex);
            }
        }
        
        public virtual UserPermissionCollection GetUserPermissionList(UserPermissionColumns orderBy, OrderDirection orderDirection)
        {            
            int totalRecords = 0;
            return GetUserPermissionList(orderBy, orderDirection, 0, 0, out totalRecords);
        }
        
        #endregion
		
		  #region GetUserPermissionList methods
        public virtual UserPermissionCollection GetUserPermissionListByParam(object orderBy, OrderDirection orderDirection, int page, int pageSize,string Criteria, out int totalRecords)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spUserPermissionGetListByParam");
                
                database.AddInParameter(dbCommand, "@OrderBy", DbType.AnsiString, orderBy.ToString());
                database.AddInParameter(dbCommand, "@OrderDirection", DbType.AnsiString, orderDirection.ToString());
                database.AddInParameter(dbCommand, "@Page", DbType.Int32, page);
                database.AddInParameter(dbCommand, "@PageSize", DbType.Int32, pageSize);
				database.AddInParameter(dbCommand, "@Criteria",  DbType.String, Criteria);
                database.AddOutParameter(dbCommand, "@TotalRecords", DbType.Int32, 4);
                
                UserPermissionCollection userPermissionCollection = new UserPermissionCollection();
                using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommand))
                {
                    while (reader.Read())
                    {
                        UserPermission userPermission = CreateUserPermissionFromReader(reader);
                        userPermissionCollection.Add(userPermission);
                    }
                    reader.Close();
                }
                totalRecords = (int)database.GetParameterValue(dbCommand, "@TotalRecords");
                return userPermissionCollection;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessGetUserPermissionListException, ex);
            }
        }
        
        public virtual UserPermissionCollection GetUserPermissionListByParam(object orderBy, OrderDirection orderDirection,string Criteria)
        {            
            int totalRecords = 0;
            return GetUserPermissionListByParam(orderBy, orderDirection, 0, 0,Criteria, out totalRecords);
        }
        
        #endregion
    }
}